package com.hk.crm.workbench.web.controller;

import com.hk.crm.VO.PaginationVO;
import com.hk.crm.settings.domain.User;
import com.hk.crm.settings.service.UserService;
import com.hk.crm.utils.DateTimeUtil;
import com.hk.crm.utils.UUIDUtil;
import com.hk.crm.workbench.domain.Activity;
import com.hk.crm.workbench.domain.Clue;
import com.hk.crm.workbench.service.ActivityService;
import com.hk.crm.workbench.service.ClueService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/workbench/clue")
public class ClueController extends HttpServlet {
    @Resource
    ClueService clueService;
    @Resource
    ActivityService proxyService;
    @Resource
    UserService userService;

    @RequestMapping("/getActivityListByNameAndNotByClueId.do")
    @ResponseBody
    private void getActivityListByNameAndNotByClueId(HttpServletRequest req, HttpServletResponse resp) {


    }
    @RequestMapping("/deleteById.do")
    @ResponseBody
    private boolean deleteById(HttpServletRequest req, HttpServletResponse resp) {
        String id=req.getParameter("id");
        boolean flag=clueService.deleteById(id);
        return flag;
    }
    @RequestMapping("/getActivityListByClueId.do")
    @ResponseBody
    private List<Activity> getActivityListByClueId(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("根据线索id查询管理的市场活动列表");
        String clueId=req.getParameter("clueId");
        List<Activity> activityList=proxyService.getActivityListByClueId(clueId);
        return activityList;
    }
    @RequestMapping("/detail.do")
    @ResponseBody
    private ModelAndView detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ModelAndView mv=new ModelAndView();
        System.out.println("进入到跳转线索信息页的操作");
        String id=req.getParameter("id");
        Clue clue=clueService.detail(id);
//        将得到的c存到request域
        mv.addObject("clue", clue);
        mv.setViewName("/clue/detail");
        return mv;
    }
    @RequestMapping("/pageList.do")
    @ResponseBody
    private PaginationVO<Clue> pageList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询线索");
        String fullname=req.getParameter("fullname");
        String company=req.getParameter("company");
        String phone=req.getParameter("phone");
        String mphone=req.getParameter("mphone");
        String source=req.getParameter("source");
        String owner=req.getParameter("owner");
        String status=req.getParameter("status");
//        代表第几页
        String pageNoStr=req.getParameter("pageNo");
        int pageNo=Integer.valueOf(pageNoStr);
//       代表每页展现几条数据
        String pageSizeStr=req.getParameter("pageSize");
        int pageSize=Integer.valueOf(pageSizeStr);
//        计算出略过的记录数
        int skipCount=(pageNo-1)*pageSize;
        Map<String ,Object> map=new HashMap();
        map.put("fullname",fullname);
        map.put("owner", owner);
        map.put("company", company);
        map.put("phone", phone);
        map.put("mphone", mphone);
        map.put("source", source);
        map.put("status", status);
        map.put("skipCount", skipCount);
        map.put("pageSize", pageSize);
/*
            前端要：市场活动信息列表
            查询的总条数
            业务层拿到了上述两项信息之后如何做返回
                map
                vo
* */
//        Service返回的是个VO，里面是从数据库得到的List<Clue> 和total 数据总数
        PaginationVO<Clue> vo=clueService.pageList(map);
//        将得到的VO转成json串传给前端
        return vo;
    }
    @RequestMapping("/saveClue.do")
    @ResponseBody
    private Map<String ,Object> saveClue(HttpServletRequest req, HttpServletResponse resp) {
        String id=UUIDUtil.getUUID();
        String createTime= DateTimeUtil.getSysTime();
        //        创建人：当前登录的用户
        String createBy=((User)req.getSession().getAttribute("user")).getNAME();
        String owner = req.getParameter("owner");
        String company = req.getParameter("company");
        String appellation = req.getParameter("appellation");
        String fullname = req.getParameter("fullname");
        String job = req.getParameter("job");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String website = req.getParameter("website");
        String mphone = req.getParameter("mphone");
        String status = req.getParameter("status");
        String source = req.getParameter("source");
        String description = req.getParameter("description");
        String contactSummary = req.getParameter("contactSummary");
        String nextContactTime = req.getParameter("nextContactTime");
        String address = req.getParameter("address");
        Clue clue=new Clue();
        clue.setId(id);
        clue.setAddress(address);
        clue.setAppellation(appellation);
        clue.setCompany(company);
        clue.setContactSummary(contactSummary);
        clue.setCreateBy(createBy);
        clue.setCreateTime(createTime);
        clue.setDescription(description);
        clue.setEmail(email);
        clue.setOwner(owner);
        clue.setFullname(fullname);
        clue.setJob(job);
        clue.setPhone(phone);
        clue.setWebsite(website);
        clue.setMphone(mphone);
        clue.setState(status);
        clue.setSource(source);
        clue.setNextContactTime(nextContactTime);
        boolean flag=clueService.saveClue(clue);
        Map<String ,Object> map=new HashMap<>();
        map.put("success", flag);
        map.put("clue", clue);
        return map;
    }
    @RequestMapping("/getUserList.do")
    @ResponseBody
    private List<User> getUserList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("取得用户信息列表");
        List<User> userList = userService.getUserList();
        return userList;
    }


}
